#include <iostream>
#include <time.h>
#include <stdlib.h>

using namespace std;

void print(bool ** vec, int tam)
{
    for(int i = 0; i < tam ; i++)
    {
        for(int j = 0; j < tam ; j++)
        {
            if(vec[i][j])
                cout << "*"<<" ";
            else
                cout << "."<<" ";
        }
        cout << endl;
    }
}

void inicializar(bool ** & vec , int tam)
{
    srand(time(NULL));
    vec = new bool *[tam];
    for(int i = 0; i < tam ; i++)
        vec[i] = new bool [tam];
    for(int i = 0; i < tam ; i++)
    {
        for(int j = 0; j < tam ; j++)
        {
            int aux = rand() % 100;
            if (aux % 2 == 0)
                vec[i][j] = 1;
            else
                vec[i][j] = 0;
        }
    }
}

bool box_disabled(bool **& vec , int tam ,int i , int j)
{
    return (i < 0 || j < 0  || j >= tam || i >= tam || !vec[i][j]);
}

int square (bool ** &vec, int tam, int i , int j)
{
    int aux = 0;
    bool square_bool[8];
    square_bool[0] = !box_disabled(vec,tam,i-1,j);
    square_bool[1] = !box_disabled(vec,tam,i+1,j);
    square_bool[2] = !box_disabled(vec,tam,i,j+1);
    square_bool[3] = !box_disabled(vec,tam,i,j-1);
    square_bool[4] = !box_disabled(vec,tam,i+1,j+1);
    square_bool[5] = !box_disabled(vec,tam,i+1,j-1);
    square_bool[6] = !box_disabled(vec,tam,i-1,j+1);
    square_bool[7] = !box_disabled(vec,tam,i-1,j-1);
    int true_box = 0;
    for(int i = 0; i < 8 ; i++ )
    {
        if (square_bool[i])
            true_box++;
    }
    return true_box;
}

int count_neighbor(bool **& vec, int tam)
{
     for(int i = 0; i < tam ; i++)
    {
        for(int j = 0; j < tam ; j++)
        {
            int aux = square(vec,tam,i,j);
            cout << "numero en " <<i << " " << j <<  "es" << aux << endl;
            if (!vec[i][j] && aux == 3)
                vec[i][j] = 1;
            else
            {
                if(vec[i][j] && (aux > 3  || aux < 2) )
                    vec[i][j] = 0;
            }
        }
    }
}

int main()
{
    bool ** vec;
    int tam = 10;
    inicializar(vec,tam);
    print(vec,tam);
    count_neighbor(vec,tam);
    cout << endl;
    print(vec,tam);

    cout << "Hello world!" << endl;
    return 0;
}
